// pages/restaurant/shop/component/productModel/productModel.js
const app = getApp();
const util = require('../../../utils/util.js')
import {addCart, getCart, update, del} from "../../../api/cart"
import {find} from "../../../api/product"

Component({
        /**
         * 组件的属性列表
         */
        properties: {
                shopId:{
                        type:String,
                        value:""
                },
                shopInfo:{
                        type:Object,
                        value:""
                },
                productInfoDeliver:{
                        type:Object,
                        observer:function(news, old, path){
                                // console.info("productInfoDeliver***********************")
                                // console.info(news)
                                this.setData({
                                        productInfo: news 
                                })
                        }
                },
                cartInfoDeliver:{
                        type:Object,
                        observer:function(news, old, path){
                                this.setData({
                                        cartInfo: news 
                                })
                        }
                },
        },

        observers: {
                'productInfo, cartInfo': function (productInfoDeliver, cartInfoDeliver) {
                        if(productInfoDeliver.hasOwnProperty("id") && cartInfoDeliver.hasOwnProperty("itemVoList")){
                                this.init(productInfoDeliver, cartInfoDeliver)
                        }           
                },
                // 'productInfo': function (productInfoDeliver) {
                //         if(productInfoDeliver.hasOwnProperty("id")){
                //                 console.log("productInfoDeliver--------------")
                //                   console.log(productInfoDeliver)
                //                 if(productInfoDeliver.sku>1){
                //                         this.getProductInfo(productInfoDeliver.id)
                //                 }
                //         }           
                // },
        },

        /**
         * 组件的初始数据
         */
        data: {
               cartInfo:{} ,
               productInfo:{},
               quantity:0,
               stock:0,
        },

        attached() { 
                
        },

        /**
         * 组件的方法列表
         */
        methods: {
                init(productInfo, cartInfo){
                        // console.info("我监控到更新了")
                        // console.info(productInfo)
                        if(productInfo && cartInfo){
                                if(cartInfo.itemVoList.length > 0){
                                        let clearOff= true
                                        let quantity = 0
                                        cartInfo.itemVoList.map(item => {
                                                if(item.goodsId == productInfo.goodsId){
                                                        quantity += item.quantity
                                                        clearOff = false   
                                                }
                                        })
                                        this.setData({
                                                quantity : quantity
                                        })
                                        if(clearOff){
                                                this.setData({
                                                        quantity : 0
                                                })
                                        }
                                }else{
                                        this.setData({
                                                quantity : 0
                                        })      
                                }
                        }
                },

                // getProductInfo(id){
                //         find(id).then(
                //                 res => {
                //                   console.log("getProductInfo--------------")
                //                   console.log(res)
                //                   if (res.type == 'success') {
                                          
                //                     }
                //                 }
                //               ).catch(error => {})
                // },

                detailsClick(){
                        wx.navigateTo({
                          url: '/pages/shop/productDetail/productDetail?productId='+this.data.productInfo.id + '&shopId=' +this.data.shopId,
                        })
                },

                getCart(){
                        let params = {
                                shopId:this.data.shopId,
                                uuid: app.globalData.uuid
                        }
                        getCart(params).then(
                                res => {
                                  console.log("getCart--" + JSON.stringify(res))
                                  if (res.type == 'success') {
                                          this.setData({
                                                cartInfo:res.data.cart
                                          })
                                    }
                                }
                              ).catch(error => {})
                },

                addCart(e){
                        let _this = this
                        console.info(e)
                        let maxLimit = e.currentTarget.dataset.item.maxLimit
                        let minLimit = e.currentTarget.dataset.item.minLimit
                        if(maxLimit == _this.data.quantity&&maxLimit>0){
                                wx.showToast({
                                  title: `该菜品限购${maxLimit}件哦！`,
                                  duration: 2000,
                                  icon: "none",
                                })
                        }else{
                        let sendData = {
                                productId: e.currentTarget.dataset.item.id,
                                type:"add",
                                shopId: _this.data.shopId, 
                                clientX: e.touches[0].clientX,
                                clientY: e.touches[0].clientY
                        }
                        if(e.currentTarget.dataset.item.sku > 1){
                                console.info("e.currentTarget.dataset.item.sku --"+e.currentTarget.dataset.item.sku )
                                _this.triggerEvent('showAttrDialog', sendData)
                        }else {
                                let params = {
                                        productId:e.currentTarget.dataset.item.id,
                                        quantity:1,
                                        shopId:_this.data.shopId,
                                        uuid: app.globalData.uuid
                                }     
                                if(_this.data.quantity < minLimit){
                                        params.quantity = minLimit
                                        if(minLimit>1){
                                                wx.showToast({
                                                        title: `该菜品${minLimit}件起购哦！`,
                                                        duration: 2000,
                                                        icon: "none",
                                                      })
                                        }
                                                 
                                }else{
                                        params.quantity = 1
                                }
                                addCart(params).then(
                                        res => {
                                        //   console.log("addCart--" + JSON.stringify(res))
                                          if (res.type == 'success') {
                                                _this.triggerEvent('reloadCart', sendData)
                                            }
                                        }
                                      ).catch(error => {})
                        }
                }
                },

                reduceCart(e){
                        console.info(e)
                        let _this = this
                        let maxLimit = e.currentTarget.dataset.item.maxLimit
                        let minLimit = e.currentTarget.dataset.item.minLimit>1?e.currentTarget.dataset.item.minLimit:1
                        let cartId = ""
                        let sendData = {
                                type:"reduce",
                                shopId: _this.data.shopId, 
                                clientX: e.touches[0].clientX,
                                clientY: e.touches[0].clientY
                        }
                        if(_this.data.quantity > 0){
                                if(_this.data.cartInfo.itemVoList.length > 0){
                                        _this.data.cartInfo.itemVoList.map(item => {
                                                if(item.productId == e.currentTarget.dataset.item.id){
                                                        cartId = item.id
                                                }
                                        })
                                }
                                if(_this.data.quantity > minLimit){
                                        let data = [{
                                                id:cartId,
                                                quantity:_this.data.quantity - 1,
                                                chk : true
                                        }]
                                        update(data).then(
                                                res => {
                                                //   console.log("addCart--" + JSON.stringify(res))
                                                  if (res.type == 'success') {
                                                        _this.triggerEvent('reloadCart',sendData)
                                                    }
                                                }
                                              ).catch(error => {})
                                }else{
                                        let delIds = [cartId]
                                        del(delIds).then(
                                                res => {
                                                  // console.log("我是组件的getCart--" + JSON.stringify(res))
                                                  if (res.type == 'success') {
                                                        _this.triggerEvent('reloadCart',sendData)
                                                  }
                                                }
                                              ).catch(error => {})
                                }
                                
                        }
                }
        }
})
